package com.situ.service.impl;

import com.situ.domain.vo.BussinessReportVO;
import com.situ.exception.ExcelEcxeption;
import com.situ.service.*;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ReportServiceImpl implements ReportService {

    private final UserService userService;

    private final OrderService orderService;

    private final OrderItemService orderItemService;

    /**
     * 运营数据统计报表
     * @return
     */
    @Override
    public BussinessReportVO getBusinessReport() {
        BussinessReportVO.UserReport userReport = userService.getBussinessUserReport();
        BussinessReportVO.OrderReport orderReport = orderService.getBussinessOrderReport();
        List<BussinessReportVO.HotProduct> hotProducts = orderItemService.getBussinessHotProduct();
        return BussinessReportVO
                .builder()
                .userReport(userReport)
                .orderReport(orderReport)
                .hotProduct(hotProducts)
                .reportDate(LocalDate.now())
                .build();
    }


    /**
     * 运营数据统计报表导出
     * @param response
     */
    @Override
    public void getBusinessReportExcel(HttpServletResponse response) {
        BussinessReportVO businessReport = this.getBusinessReport();
        InputStream inputStream =
                this.getClass()
                        .getClassLoader()
                        .getResourceAsStream("template/运营数据报表模板.xlsx");
        try {
            XSSFWorkbook excel = new XSSFWorkbook(inputStream);
            XSSFSheet sheet1 = excel.getSheet("Sheet1");
            // 向excel中写数据
            sheet1.getRow(1).getCell(0).setCellValue("日期：" + businessReport.getReportDate().toString());
            sheet1.getRow(3).getCell(3).setCellValue(businessReport.getUserReport().getTodayNewMember());
            sheet1.getRow(3).getCell(7).setCellValue(businessReport.getUserReport().getTotalMember());
            sheet1.getRow(4).getCell(3).setCellValue(businessReport.getUserReport().getThisWeekNewMember());
            sheet1.getRow(4).getCell(7).setCellValue(businessReport.getUserReport().getThisMonthNewMember());

            sheet1.getRow(6).getCell(3).setCellValue(businessReport.getOrderReport().getTodayNewOrder());
            sheet1.getRow(6).getCell(7).setCellValue(businessReport.getOrderReport().getThisWeekNewOrder());

            List<BussinessReportVO.HotProduct> hotProduct = businessReport.getHotProduct();
            for (int i = 0; i < hotProduct.size(); i++) {
                BussinessReportVO.HotProduct item = hotProduct.get(i);
                sheet1.getRow(9 + i).getCell(0).setCellValue(item.getTitle());
                sheet1.getRow(9 + i).getCell(4).setCellValue(item.getTotalNum());
                sheet1.getRow(9 + i).getCell(5).setCellValue(item.getPresent() + "%");
                sheet1.getRow(9 + i).getCell(6).setCellValue(item.getSellPoint());
            }

            // 返回数据
            ServletOutputStream outputStream = response.getOutputStream();
            //设定响应数据的头信息 响应的数据格式是excel文件
            response.setContentType("application/vnd.ms-excel");
            //响应头中设定输出文件名称
            response.setHeader("Content-Disposition", "attachment; filename=business.xlsx");
            excel.write(outputStream);
            outputStream.flush();
            outputStream.close();
            excel.close();
        } catch (IOException e) {
            throw new ExcelEcxeption(e);
        }
    }
}
